package com.tank;

import java.awt.*;

public class Bullet {

    private final static int SPEED = 3;
    private static int WIDTH = 20,HEIGHT=20;
    private int x, y;
    private Dir dir;

    private boolean live = true;

    TankFrame tankFrame = null;

    public Bullet(int x, int y, Dir dir, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
        System.out.println("bullet dir: " + dir);
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public void paint(Graphics graphics){
        graphics.setColor(Color.RED);
        graphics.fillOval(x, y,WIDTH,HEIGHT);
        move();
    }

    private void move(){
        if(!live){
            tankFrame.bullets.remove(this);
        }
        switch (dir){
            case UP:
                y -=SPEED;
                break;
            case DOWN:
                y +=SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
        }
        if(x<0 || y<0 || x>tankFrame.GAME_WIDTH || y>tankFrame.GAME_HEIGHT) live = false;
    }

}
